

import numpy


class NMerGen(object):
    
    def __init__(self,**kwargs):
        self.nmer   = 3
        self.dna    = ['A','C','G','T']
        self.__dict__.update(kwargs)        
                    
    def getNmer(self):
        #res -> ['AA','AC','AG', ..]
        self.res    = []
        #dlevel -> [['A', 'C', 'T', 'G'], ['A', 'C', 'T', 'G'],..]
        dlevel      = []
        cv          = numpy.zeros(self.nmer)  
        #Make every level
        for i in range(self.nmer): #@UnusedVariable
            dlevel.append(self.dna)
        track       = 0
        rcnt        = 0
        #g -> ['A','A']
        g           = []     
        k0          = 1
        while k0:                        
            #cv[track] -> [0,0][track]
            t       = self.dna[int(cv[track])]
            g.append(t)
            track   += 1
            if   track == self.nmer:
                track       = 0
                #assembly -> 'AA'
                assembly    = ''.join(g)
                #reset g
                g           = []
                self.res.append(assembly)
                rcnt        += 1
                cnt         = -1 #Start with the last item on dlevel
                k1          = 1
                while k1:
                    cv[cnt]     += 1
                    if cv[cnt] == len(self.dna):
                        cv[cnt] = 0
                        cnt     -= 1
                        if abs(cnt) > self.nmer:
                            k1  = 0
                    else:
                        k1      = 0          
            if rcnt  >= 4**self.nmer:
                k0          = 0                        
#        for i in self.res:
#            print i 
#        print len(self.res)